const http = require('http');
const httpProxy = require('http-proxy');
var proxy = httpProxy.createServer({});
proxy.on('error', function (err, req, res) {
    res.writeHead(500, {
        'Content-Type': 'text/plain'
    });

    res.end('Something went wrong. And we are reporting a custom error message.');
});
var server = http.createServer(function (req, res) {
    var host = req.headers.host, ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress;
    console.log(host, ip,'************************========');
    req.headers['x-forwarded-for'] = ip;
    switch (host){
        case 'manage.bandtechs.com':
            proxy.web(req, res, {target: 'http://47.100.1.219:3000'});
            break;
        case 'product.bandtechs.cn':
            proxy.web(req, res, {target: 'http://47.100.1.219:4000'});
            break;
        case 'qrcode.bandtechs.cn':
            proxy.web(req, res, {target: 'http://47.100.1.219:5000'});
            break;
        default:
            proxy.web(req, res, {target: 'http://localhost:82'});
            break;
    }
});
server.listen(80);
console.log('proxy listening on port 80');